package network;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.net.InetAddress;
import java.net.UnknownHostException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

public class InettAddressEx extends JFrame {

	private JPanel contentPane;
	private JPanel panel;
	private JButton btnNewButton;
	private JTextField domain;
	private JScrollPane scrollPane;
	private JTextArea result;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					InettAddressEx frame = new InettAddressEx();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public InettAddressEx() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		contentPane.add(getPanel(), BorderLayout.NORTH);
		contentPane.add(getScrollPane(), BorderLayout.CENTER);
	}
	private JPanel getPanel() {
		if (panel == null) {
			panel = new JPanel();
			panel.setLayout(new BorderLayout(0, 0));
			panel.add(getBtnNewButton(), BorderLayout.EAST);
			panel.add(getDomain(), BorderLayout.CENTER);
		}
		return panel;
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("확인");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					
					domain();
				}
			});
		}
		return btnNewButton;
	}
	private JTextField getDomain() {
		if (domain == null) {
			domain = new JTextField();
			domain.addKeyListener(new KeyAdapter() {
				@Override
				public void keyPressed(KeyEvent ev) {
					if(ev.getKeyCode() == KeyEvent.VK_ENTER)
					domain();
				}
				
			});
			domain.setColumns(10);
		}
		return domain;
	}
	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setViewportView(getResult());
		}
		return scrollPane;
	}
	private JTextArea getResult() {
		if (result == null) {
			result = new JTextArea();
		}
		return result;
	}
	public void domain(){
		String a = domain.getText();
		try{
			InetAddress ia =InetAddress.getByName(a);
			String IP =ia.getHostAddress();
			result.append(a+":"+IP+"\n");
			InetAddress ias[] =InetAddress.getAllByName(a);
			for(int i=0; i<ias.length;i++){
				String ip = ias[i].getHostAddress();
				result.append("["+(i+1)+"]"+a+":"+ip+"\n");
			}
			InetAddress local = InetAddress.getLocalHost();
			result.append("ip:"+local.getHostAddress()+"\n");
			

		}catch(UnknownHostException ue){
			System.out.println(ue.getMessage());
		}
	}
}
